(function ($) { })(jQuery);

// var _yunNotyBool = 0;
//     if (data.status == -1) {
//         data.clallback = function () {
//             var fromurl = top.location.href;
//             var urlId = "";
//             if (fromurl && fromurl.indexOf("#") < 0) {
//                 urlId = fromurl.substring(fromurl.indexOf("#") + 1, fromurl.length);
//                 if (urlId.length > 10) {
//                     urlId = "";
//                 }
//             }
//             top.location.href = "/login.html?from=" + urlId;
//         };
//     }
//     var option = {
//         title: "提醒",
//         text: data.message,
//         time: data.timeout ? data.timeout : 3000,
//         class_name: data.status == 0 ? "" : "gritter-light",
//         after_close: data.clallback ? data.clallback : false
//     };
//     if (stick === true) {
//         option = $.extend(option, { sticky: true });
//     }
//     if (!_yunNotyBool) $.gritter.add(option);
//     if (data.status == -1) _yunNotyBool = 1;
//     if (typeof fun == 'function') {
//         setTimeout(fun, 1500);
//     } else {}


function yunNotyError(msg, fun, stick) {
    var option = {
        title: "提醒",
        text: msg,
        time: 4000,
        class_name: "gritter-light"
    };
    if (stick === true) {
        option = $.extend(option, { sticky: true });
    }
    $.gritter.add(option);
    if (typeof fun == 'function') {
        setTimeout(fun, 1800);
    } else { }
}

//截取字符串
function limitstr(chinese_string, len) {
    if (!chinese_string) return;
    if (chinese_string.length > len) {
        var new_text = chinese_string.substring(0, len) + "...";
        return new_text;
    }
    return chinese_string;
}

//设置ajax分页
function setPage(domId, options) {
    $('#' + domId).bootstrapPaginator(options);
}

//icheck
function icheckBindInit() {
    $('table').on('ifClicked', '.select_rows',
        function () {
            var tableid = $(this).data('tableid');
            var ckbs = $('#' + tableid).find('input[name=ckb]').filter(function (ind, el) {
                return $(el).data('del') != 1;
            });
            if ($(this)[0].checked) {
                ckbs.iCheck('uncheck');
            } else {
                ckbs.iCheck('check');
            }
        });
}

function icheckListInit() {
    //不勾选全选
    $('.select_rows').iCheck('uncheck');
    $('.select_row').on('ifUnchecked',
        function () {
            $('.select_rows').iCheck('uncheck');
        });
    //初始化ickeck
    icheckInit();
}

//获取部分选中的checkbox的value
function getSelectedIds() {
    var cboxs = document.getElementsByName('ckb');
    if (typeof cboxs == "undefined") {
        return "";
    }
    var inputvalue = "";

    for (var i = 0; i < cboxs.length; i++) {
        if (cboxs[i].checked === true) {
            inputvalue += cboxs[i].value + ",";
        }
    }
    inputvalue = inputvalue.substring(0, inputvalue.length - 1);
    return inputvalue;
}

//修改或进行其他操作后展示当前页
function listCurrentPage(listFun, pageListID) {
    var pageNo = $('#' + pageListID + ' .active a').html();
    listFun(pageNo);
}

//通用的批量删除方法
function selectDel(ids, url, fun, pageId) {
    if (ids === '') {
        ids = getSelectedIds();
    }
    if (ids === "") {
        return false;
    }
    if (url === "") {
        return false;
    }
    $.getJSON(url, "ids=" + ids,
        function (data) {
            if (data.status === 0) {
                var ids_array = ids.split(",");
                for (i = 0; i < ids_array.length; i++) {
                    $('#list-tr-' + ids_array[i]).hide();
                }
                if (typeof fun == "function") {
                    if (pageId) {
                        var page = $('#' + pageId + ' .active a').html();
                        var oT = $('input[name=orderType]').val();
                        if ($('.m-del') !== undefined) {
                            if ($('.m-del').size() == ids_array.length) page -= 1;
                            if (page < 1) page = 1;
                        }
                        if (oT) {
                            fun(page, oT);
                        } else {
                            fun(page);
                        }

                    } else {
                        fun();
                    }
                }
                $('.select_rows').attr('checked', false);
                yunNoty(data);
            } else {
                yunNoty(data);
            }
        });
    return false;
}

//接待入口管理批量删除传id
function selectDel2(id, url, fun, pageId) {
    if (id === '') {
        id = getSelectedIds();
    }
    if (id === "") {
        return false;
    }
    if (url === "") {
        return false;
    }
    $.getJSON(url, "id=" + id,
        function (data) {
            if (data.status === 0) {
                var id_array = id.split(",");
                for (i = 0; i < id_array.length; i++) {
                    $('#list-tr-' + id_array[i]).hide();
                }
                if (typeof fun == "function") {
                    if (pageId) {
                        var page = $('#' + pageId + ' .active a').html();
                        var oT = $('input[name=orderType]').val();
                        if ($('.m-del') !== undefined) {
                            if ($('.m-del').size() == ids_array.length) page -= 1;
                            if (page < 1) page = 1;
                        }
                        if (oT) {
                            fun(page, oT);
                        } else {
                            fun(page);
                        }

                    } else {
                        fun();
                    }
                }
                $('.select_rows').attr('checked', false);
                yunNoty(data);
            } else {
                yunNoty(data);
            }
        });
    return false;
}

//通用的单个删除方法
var _customGetJSONFlag = false;
function delById(obj, url, fun, pageId) {
    var $this = $(obj);
    if (typeof url == 'undefined') {
        return;
    }
    if (typeof pageId == 'undefined') {
        return;
    }
    if (_customGetJSONFlag) {
        return;
    }
    _customGetJSONFlag = true;
    $.getJSON(url, 'ids=' + $this.attr('rel') + '&id=' + $this.attr('rel'),
        function (data) {
            setTimeout(function () { _customGetJSONFlag = false; }, 1000);
            if (data.status === 0) {
                if (typeof fun == "function") {
                    var page = $('#' + pageId + ' .active a').html();
                    if ($('.m-del') !== undefined) {
                        if ($('.m-del').size() == 1) page -= 1;
                        if (page < 1) page = 1;
                    }
                    fun(page);
                }
                yunNoty(data);
            } else {
                yunNoty(data);
            }
        });
}

//修改列表模态框保存按钮
function saveModal(url, formId, modalId, fun, pageId) {
    $.ajax({
        type: 'post',
        datatype: 'json',
        cache: false,
        //不从缓存中去数据
        url: encodeURI(url),
        data: $("#" + formId).serialize(),
        success: function (data) {
            if (data.status === 0) {
                $('#' + modalId).modal('hide');
                if (typeof fun == "function") {
                    var page = $('#' + pageId + ' .active a').html();
                    var oT = $('input[name=orderType]').val();
                    if (!oT) {
                        fun(page);
                    } else if (oT && pageId) {
                        fun(page, oT);
                    } else {
                        fun();
                    }
                }
                fun();
                yunNoty(data);
            } else {
                fun();
                yunNoty(data);
            }
        },
        error: function (err) {
            fun(err);
        }
    });
}

//简单的禁用、启用等的方法
function simpleEdit(url, obj, fun, pageId, status, ids) {
    if (typeof status == "undefined") status = '';
    if (typeof ids == "undefined") ids = '';
    var curObj = $(obj).attr('rel');
    if (typeof curObj == "undefined") curObj = '';
    $.ajax({
        type: 'post',
        datatype: 'json',
        cache: false,
        //不从缓存中去数据
        url: encodeURI(url),
        data: 'id=' + curObj + '&status=' + status + '&ids=' + ids,
        success: function (data) {
            if (data.status === 0) {
                if (typeof fun == "function") {
                    var page = $('#' + pageId + ' .active a').html();
                    var oT = $('input[name=orderType]').val();
                    if (!oT) {
                        fun(page);
                    } else {
                        fun(page, oT);
                    }
                }
                yunNoty(data);
            } else {
                yunNoty(data);
            }
        }
    });
}

//配置参数请求地址
function request(obj, url, fun) {
    $.ajax({
        type: 'get',
        datatype: 'json',
        cache: false,
        //不从缓存中去数据
        url: encodeURI(url),
        data: $(obj).parents('form').serialize(),
        success: function (data) {
            if (data.status === 0) {
                if (typeof fun == "function") {
                    fun();
                }
                yunNoty(data);
            } else {
                yunNoty(data);
            }

        }
    });
}

//过滤标签
function $xss(str, type) {
    //空过滤
    if (!str) {
        return str === 0 ? "0" : "";
    }
    switch (type) {
        case "none":
            //过度方案
            return str + "";
        case "html":
            //过滤html字符串中的XSS
            return str.replace(/[&'"<>\/\\\-\x00-\x09\x0b-\x0c\x1f\x80-\xff]/g,
                function (r) {
                    return "&#" + r.charCodeAt(0) + ";";
                }).replace(/ /g, " ").replace(/\r\n/g, "<br />").replace(/\n/g, "<br />").replace(/\r/g, "<br />");
        case "htmlEp":
            //过滤DOM节点属性中的XSS
            return str.replace(/[&'"<>\/\\\-\x00-\x1f\x80-\xff]/g,
                function (r) {
                    return "&#" + r.charCodeAt(0) + ";";
                });
        case "url":
            //过滤url
            return escape(str).replace(/\+/g, "%2B");
        case "miniUrl":
            return str.replace(/%/g, "%25");
        case "script":
            return str.replace(/[\\"']/g,
                function (r) {
                    return "\\" + r;
                }).replace(/%/g, "\\x25").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\x01/g, "\\x01");
        case "reg":
            return str.replace(/[\\\^\$\*\+\?\{\}\.\(\)\[\]]/g,
                function (a) {
                    return "\\" + a;
                });
        default:
            return escape(str).replace(/[&'"<>\/\\\-\x00-\x09\x0b-\x0c\x1f\x80-\xff]/g,
                function (r) {
                    return "&#" + r.charCodeAt(0) + ";";
                }).replace(/ /g, " ").replace(/\r\n/g, "<br />").replace(/\n/g, "<br />").replace(/\r/g, "<br />");
    }
}

//展开分类
$(".expandAll").bind("click", expandAll);
function expandAll() {
    var zTree = $.fn.zTree.getZTreeObj("treeClasses");
    zTree.expandAll(true);
    return false;
}

$(".expandNot").bind("click", expandNot);
function expandNot() {
    var zTree = $.fn.zTree.getZTreeObj("treeClasses");
    zTree.expandAll(false);
    return false;
}

$(".expandListAll").bind("click", expandAll);
function expandAll() {
    var zTree = $.fn.zTree.getZTreeObj("treeClasses");
    zTree.expandAll(true);
    return false;
}

//资源，角色富名称tree展开节点
function showTree(treeId, flag) {
    var rolezTree = $.fn.zTree.getZTreeObj(treeId);
    rolezTree.expandAll(flag);
    return false;
}

//资源，角色生效时间问题
function setTimeValid(formId) {
    if (!formId) formId = '';
    $('#' + formId + ' input[name=timeLiness_temp]').on('ifChecked',
        function () {
            $('#' + formId + ' input[name=timeLiness_temp]').val(1);
        });
}

function setTimeUnValid(formId) {
    if (!formId) formId = '';
    $('#' + formId + ' input[name=timeLiness_temp]').on('ifUnchecked',
        function () {
            $('#' + formId + ' input[name=timeLiness_temp]').val(0);
        });
}


//验证url
function validUrl(value) {
    // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
    return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
}

//验证电话号码
function validTel(telValue) {
    var isPhone = /^(1[3,5,8,7]{1}[\d]{9})|(((400)-(\d{3})-(\d{4}))|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{3,7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
    if (!isPhone.test(telValue)) { //如果用户输入的值不同时满足手机号和座机
        return false;
    } else {
        return true;
    }
}

//转化为 日+小时+分+秒
function formatTime(longTime) {
    var time = parseFloat(longTime);
    if (time !== null && time !== "") {
        if (time < 60) {
            var s = time;
            time = s + '秒';
        } else if (time >= 60 && time < 3600) {
            var m = parseInt(time / 60);
            var s1 = parseInt(time % 60);
            time = m + "分钟" + s1 + "秒";
        } else if (time >= 3600 && time < 86400) {
            var hh = parseInt(time / 3600);
            var mm = parseInt(time % 3600 / 60);
            var ss = parseInt(time % 3600 % 60 % 60);
            time = hh + "小时" + mm + "分钟" + ss + "秒";
        } else if (time >= 86400) {
            var d4 = parseInt(time / 86400);
            var h4 = parseInt(time % 86400 / 3600);
            var m4 = parseInt(time % 86400 % 3600 / 60);
            var s4 = parseInt(time % 86400 % 3600 % 60 % 60);
            time = d4 + '天' + h4 + "小时" + m4 + "分钟" + s4 + "秒";
        }
    }
    return time;
}

//资源，角色删除事件
function delData(delFormId, url, modalId, cb, treeId) {
    $.ajax({
        type: 'post',
        datatype: 'json',
        cache: false,
        url: encodeURI(url),
        data: $('#' + delFormId).serialize(),
        success: function (data) {
            if (data.status === 0) {
                yunNoty(data);
                $('#' + modalId).modal('hide');
                cb && cb();
                if (treeId) {
                    var zTree = $.fn.zTree.getZTreeObj(treeId);
                    zTree.reAsyncChildNodes(null, "refresh");
                }
            } else {
                yunNoty(data);
            }
        }
    });
}

//判断元素是否存在另一个元素中
function contains(array, testId) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == testId) {
            return true;
        }
    }
    return false;
}
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r !== null) return decodeURIComponent(r[2]); return null; //返回参数值
}
//按回车键提交表单  xgn
function enterSubmit(obj, fun) {
    if (!obj) return;
    if (!fun) return;
    obj.keydown(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            fun();
            return;
        }
    });
}
//此处将来访者渠道和生效渠道区分开   xgn
function showRule(data, sourceList) {
    var ruleQDHtml = '';//渠道
    var ruleVisiterHtml = '';//角色
    var ruleHtml = '';
    var ruleArea = '';//区域
    var ruleParent = '';//维度父级
    if (data.ListRule) {
        if (data.ListRule[0]) {
            for (var k = 0; k < data.ListRule.length; k++) {
                if (data.ListRule[k].ruleMode == 0) {
                    if (data.ListRule[k].type == 1) {
                        $.each(data.ListRule[k].roleIds.split(','), function () {
                            for (var m in sourceList) {
                                if (sourceList[m].DicCode == this) {
                                    ruleQDHtml += sourceList[m].DicDesc + ',';
                                }
                            }
                        });
                    } else if (data.ListRule[k].type == 2) {
                        for (var m in data.ListRule[k].roleIds) {
                            for (var n in data.ListRule[k].roleIds[m]) {
                                ruleVisiterHtml += data.ListRule[k].roleIds[m][n] + ',';
                            }
                        }
                    }
                } else if (data.ListRule[k].ruleMode == 1) {
                    /*
					 * taskId = 480 自如知识库同步，问答总览增加区域维度展示
					 * 根据ruleMode来判断，如果为0则展示生效角色，如果为1则展示区域维度
					 * 通过type判断父级来对应展示
					 * */
                    for (var z = 0; z < data.listAnswerLaber.length; z++) {
                        var ruleSon = '';//维度子级
                        if (data.ListRule[k].type == data.listAnswerLaber[z].Id) {
                            for (var m in data.ListRule[k].roleIds) {
                                for (var n in data.ListRule[k].roleIds[m]) {
                                    ruleSon += data.ListRule[k].roleIds[m][n] + ',';
                                }
                            }
                            ruleParent = data.listAnswerLaber[z].Name;
                            if (ruleSon && ruleParent) {
                                ruleSon = ruleSon.replace(/,$/, '');
                                ruleArea += ruleParent + '：' + ruleSon + '；';
                            }
                        }
                    }
                }
            }
        }
    }
    if (ruleQDHtml) {
        ruleQDHtml = ruleQDHtml.replace(/,$/, '');
        ruleHtml += '生效渠道:' + ruleQDHtml;
    }

    if (ruleVisiterHtml) {
        ruleVisiterHtml = ruleVisiterHtml.replace(/,$/, '');
        ruleHtml += (ruleHtml ? '<span class="dot">|</span>' : '') + '来访者角色：' + ruleVisiterHtml;
    }
    if (ruleArea) {
        ruleArea = ruleArea.substring(0, ruleArea.length - 1);
        ruleHtml += (ruleHtml ? '<span class="dot">|</span>' : '') + ruleArea;
    }
    ruleHtml = ruleHtml.replace(/,$/, '');
    if (ruleHtml === '') {
        return '';
    } else {
        return ruleHtml + '<span class="dot">|</span>';
    }

}


function showQuDao(data, sourceList) {
    var ruleQDHtml = '';//渠道
    var ruleVisiterHtml = '';//角色
    var ruleHtml = '';
    if (data.ListRule) {
        if (data.ListRule[0]) {
            for (var k = 0; k < data.ListRule.length; k++) {
                if (data.ListRule[k].ruleMode == 0) {
                    if (data.ListRule[k].type == 1) {
                        $.each(data.ListRule[k].roleIds.split(','), function () {
                            for (var m in sourceList) {
                                if (sourceList[m].DicCode == this) {
                                    ruleQDHtml += sourceList[m].DicDesc + ',';
                                }
                            }
                        });
                    } else {
                        if (data.ListRule[k]) {
                            for (var m in data.ListRule[k].roleIds) {
                                for (var n in data.ListRule[k].roleIds[m]) {
                                    ruleVisiterHtml += data.ListRule[k].roleIds[m][n] + ',';
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    ruleQDHtml = ruleQDHtml.replace(/,$/, '');
    if (ruleQDHtml) {
        ruleHtml += '生效渠道:' + ruleQDHtml + '<span class="dot">|</span>';
    }
    return ruleHtml;
}
function showJueSe(data, sourceList) {
    var ruleQDHtml = '';//渠道
    var ruleVisiterHtml = '';//角色
    var ruleHtml = '';
    if (data.ListRule) {
        if (data.ListRule[0]) {
            for (var k = 0; k < data.ListRule.length; k++) {
                if (data.ListRule[k].ruleMode == 0) {
                    if (data.ListRule[k].type == 1) {
                        $.each(data.ListRule[k].roleIds.split(','), function () {
                            for (var m in sourceList) {
                                if (sourceList[m].DicCode == this) {
                                    ruleQDHtml += sourceList[m].DicDesc + ',';
                                }
                            }
                        });
                    }else {
                        if(data.ListRule[k].type == 2){//taskid = 413 来访者角色type固定为2
                            for(var m in data.ListRule[k].roleIds) {
                                for(var n in data.ListRule[k].roleIds[m]) {
                                    ruleVisiterHtml += data.ListRule[k].roleIds[m][n] +',';
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    ruleVisiterHtml = ruleVisiterHtml.replace(/,$/, '');
    if (ruleVisiterHtml) {
        ruleHtml += '来访者角色：' + ruleVisiterHtml + '<br>';
    }
    return ruleHtml;
}
/*
 * taskId = 480 自如知识库同步，问答总览增加区域维度展示
 * 根据ruleMode来判断，如果为0则展示生效角色，如果为1则展示区域维度
 * 通过type判断父级来对应展示  添加方法showQuYu
 * */
function showQuYu(data, sourceList) {
    var ruleArea = '';//区域
    var ruleParent = '';//维度父级
    if (data.ListRule) {
        if (data.ListRule[0]) {
            for (var k = 0; k < data.ListRule.length; k++) {
                if (data.ListRule[k].ruleMode == 1) {
                    for (var z = 0; z < data.listAnswerLaber.length; z++) {
                        var ruleSon = '';//维度子级
                        if (data.ListRule[k].type == data.listAnswerLaber[z].Id) {
                            for (var m in data.ListRule[k].roleIds) {
                                for (var n in data.ListRule[k].roleIds[m]) {
                                    ruleSon += data.ListRule[k].roleIds[m][n] + ',';
                                }
                            }
                            ruleParent = data.listAnswerLaber[z].Name;
                            if (ruleSon && ruleParent) {
                                ruleSon = ruleSon.replace(/,$/, '');
                                ruleArea += ruleParent + '：' + ruleSon + '<br />';
                            }
                        }

                    }
                }
            }
        }
    }
    return ruleArea;

}
$.fn.addWordCount = function (num) {
    var self = this;
    if (num === undefined) {
        num = 500;
    }
    if (self[0] === undefined) {
        return false;
    }
    if (self[0].tagName !== 'INPUT' && self[0].tagName !== 'TEXTAREA') {
        return false;
    }
    function genWordCount() {
        var HTML = '';
        /**
         * taskid1763 zhaoyongping
         * 未知为题添加备注换行符算两个字符
         */
        var len = self.val().length;

        var regexp = self.val().match(/\n/g);

        if(regexp && regexp.length > 0){
            len = self.val().length;
        }

        if (len > num) {
            HTML = '<span style="color:#EA5200">' + len + '/0</span>';
        } else {
            HTML = len + '/' + (num - self.val().length);
        }
        // if (self.val().length > num) {
        //     HTML = '<span style="color:#EA5200">' + self.val().length + '/0</span>';
        // } else {
        //     HTML = self.val().length + '/' + (num - self.val().length);
        // }

        

        if (self.parent().children('[wordcount=true]').length) {
            self.parent().children('.wordCountTag').html(HTML);
        } else {
            self.after('<span class="wordCountTag pull-right" wordcount="true">' + HTML + '</span>');
        }
    }
    self.bind('input propertychange', function () {
        genWordCount();
    });
    setInterval(function () {
        genWordCount();
    }, 1200);
};

function getSourceName(sourceId) {
    if (typeof sourceId != 'number' && typeof sourceId != 'string') return '';
    if (window._ywSource) {
        if (window._ywSource[sourceId] == undefined) {
            //当配置的渠道里面没有匹配时，按sourceId来取值
            if (sourceId == -1) {
                return '全部渠道'
            } else if (sourceId == 0) {
                return '网页'
            } else if (sourceId == 1) {
                return '微信'
            } else if (sourceId == 2) {
                return 'API'
            } else if (sourceId == 3) {
                return 'APP'
            } else if (sourceId == 4) {
                return '微博'
            } else if (sourceId == 5) {
                return '支付宝'
            } else if (sourceId == 6) {
                return '手机'
            } else if (sourceId == 7) {
                return 'IOS'
            } else if (sourceId == 8) {
                return 'Android'
            } else if (sourceId == 9) {
                return '大屏端'
            } else if (sourceId == 10) {
                return '云问网页'
            } else if (sourceId == 11) {
                return '钉钉'
            } else if (sourceId == 12) {
                return 'v4网页'
            }
        } else {
            //当配置的渠道里面对应匹配到值时，按匹配的内容来取值
            return window._ywSource[sourceId];
        }
    } else {
        var temp = '';
        $.ajax({
            async: false,
            cache: false,
            dataType: 'json',
            url: '../../Configuration/showSourceByWebId',
            success: function (data) {
                var arr = {};
                if (data.listSource && data.listSource[0]) {
                    data.listSource.forEach(function (el) {
                        arr[el.DicCode] = el.DicDesc;
                    });
                }
                window._ywSource = arr
                temp = window._ywSource[sourceId] || '全部';
            }
        });
        return temp;
    }
}

function getSessionStorage(teststr) {
    if (sessionStorage) {
        var str = sessionStorage.getItem(teststr);
        if (str !== '' && str !== 'null' && str !== 'undefined') {
            return str;
        }
    }
    return '';
}

$.fn.addDeleteConfirm = function (cb) {
    var self = this;
    var pos = $(self).position();
    var $delCheck = $('<div id="ADCCheck" style="position:absolute;z-index:6667;" />').css('top', pos.top - 40).css('left', pos.left - 42).append('<div style="background-color: #ff9101;border-radius: 2px;color: #fff;padding: 5px;">是否确定删除？</div>');
    var $delInfo = $('<div id="ADCInfo" style="position:absolute;z-index:6667;width:100px;" />').css('top', pos.top).css('left', pos.left - 42).append('<button id="btn-ok-ensure" class="btn btn-xs btn-danger" style="margin-right:27px;"><i class="icon icon-check-circle"></i>确定</button><button id="btn-cancel-ensure" class="btn btn-xs btn-primary"><i class="icon icon-times-circle"></i>取消</button>')
    var $delBD = $('<div id="ADCBackDrop" style="position:absolute;width:100%;z-index:6666;cursor:pointer;top:0;left:0;background:rgba(0,0,0,0.5);" />').css('height', $(document).height());
    $delBD.append($delCheck).append($delInfo);
    $delBD.hide().appendTo($('body')).fadeIn('300');
    $('#btn-cancel-ensure').add('#ADCBackDrop').off('click').on('click', function () {
        $('#ADCBackDrop').fadeOut('250').remove();
    });
    $('#btn-ok-ensure').off('click').on('click', function () {
        cb(self);
        $('#ADCBackDrop').fadeOut('250').remove();
    });
};

$.fn.adcCreator = function (cb, bb, message) {
    if (message == undefined) {
        message = {
            title: '确认删除？',
            content: '确定要删除您所选的内容？'
        };
    } else {
        if (!message.title) message.title = '确认删除？';
        if (!message.content) message.content = '确定要删除您所选的内容？';
    }
    if (typeof bb == 'function') {
        if (!bb(this)) {
            return false;
        }
    }
    var self = this;
    var $delBD = $('<div class="modal" id="ADCDELModal" aria-hidden="false"><div class="modal-backdrop fade in" style="height: 5000px" id="ADCBackDrop"></div><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title">' + (message.title) + '</h4></div><div class="modal-body"><p>' + (message.content) + '</p></div><div class="modal-footer"><a href="javascript:;" class="btn btn-sm btn-primary" id="adc-ok-ensure">确认</a> <a href="javascript:;" class="btn btn-sm btn-white" id="adc-cancel-ensure">取消</a></div></div></div></div>');
    $delBD.hide().appendTo($('body')).fadeIn('300');

    $('#adc-cancel-ensure').add('#ADCBackDrop');
    $('body').undelegate('#adc-cancel-ensure', 'click').delegate('#adc-cancel-ensure', 'click', function () {
        $('#ADCDELModal').fadeOut('250').remove();
    })
    $('body').undelegate('#adc-ok-ensure', 'click').delegate('#adc-ok-ensure', 'click', function () {
        cb(self);
        $('#ADCDELModal').fadeOut('250').remove();
    })

}

$.fn.tableAjaxLoader1 = function (colspan, timeout) {
    var self = this;
    if (colspan == undefined) colspan = 1;
    if (timeout == undefined) timeout = 300;
    //setTimeout(function(){
    //if($(self).find('tbody').find('tr').length == 0) {
    $(self).find('tbody').empty().html('<tr style="background:#fff;"><td colspan="' + colspan + '"><div class="loading-custom"></div></td></tr>');
    //}
    //},timeout);
}

$.fn.tableAjaxLoader2 = function (colspan, timeout) {
    var self = this;
    if (colspan == undefined) colspan = 1;
    if (timeout == undefined) timeout = 300;
    if ($(self).find('th').length && $(self).find('th').length > 0) colspan = $(self).find('th').length;
    //setTimeout(function(){
    //if($(self).find('tbody').find('tr').length == 0) {
    $(self).find('tbody').empty().html('<tr style="background:#fff;"><td colspan="' + colspan + '"><div class="ajax-spinner-bars"><div class="bar-1"></div><div class="bar-2"></div><div class="bar-3"></div><div class="bar-4"></div><div class="bar-5"></div><div class="bar-6"></div><div class="bar-7"></div><div class="bar-8"></div><div class="bar-9"></div><div class="bar-10"></div><div class="bar-11"></div><div class="bar-12"></div><div class="bar-13"></div><div class="bar-14"></div><div class="bar-15"></div><div class="bar-16"></div></div></td></tr>');
    //}
    //},timeout);
}

function ifbOpenWindowInNewTab(url, title, id) {
    if (id == undefined) {
        id = 'ifbOpenWindowInNewTabId'
    }
    $('#' + id).remove();

    if(!(/^http:\/\/|https:\/\//.test(url))){
        url = window.location.protocol + "//" + window.location.host + (localStorage.getItem('Subdomain') || "") + url;
    }
    if (iframeTab) {
        if (window.top.location.href != window.location.href) {
            $('body').append('<a href="' + url + '" data-num="0" data-name="' + title + '" style="display:none;" id="' + id + '">' + title + '</a>');
            iframeTab.init({
                iframeBox: ''
            });
            $('#' + id).trigger('click');
        } else {
            location.href = url;
        }
    } else {
        location.href = url;
    }
}
/**taskId=558 智能推荐优化 add by 赵宇星
 * 说明：过滤数据
 * @data 传入需要过滤的数据
*/
function dataFilter(data) {
    if (!data) {
        data = 0;
    }
    return data;
}

/*用正则表达式实现html转码*/
function htmlEncodeByRegExp(str){  
	var s = "";
	if(str.length == 0) return "";
	s = str.replace(/&/g,"&amp;");
	s = s.replace(/</g,"&lt;");
	s = s.replace(/>/g,"&gt;");
	s = s.replace(/ /g,"&nbsp;");
	s = s.replace(/\'/g,"&#39;");
	s = s.replace(/\"/g,"&quot;");
	return s;  
}

/*用正则表达式实现html解码*/
function htmlDecodeByRegExp(str){  
	var s = "";
	if(str.length == 0) return "";
	s = str.replace(/&amp;/g,"&");
	s = s.replace(/&lt;/g,"<");
	s = s.replace(/&gt;/g,">");
	s = s.replace(/&nbsp;/g," ");
	s = s.replace(/&#39;/g,"\'");
	s = s.replace(/&quot;/g,"\"");
	return s;  
}
/**
* @param {Object} res 接口回调返回值
* @param {Function} callback 错误信息的回调方法
* @desc 根据axios(批量)下载文件
*/
function downloadFileById(id, title) {
	var title = title || "未知文件"
	var req = new XMLHttpRequest();
	req.open("post", "../../material/downloadFileById/" + id, true);
	req.responseType = "blob";
	req.setRequestHeader("Content-type", 'application/json');
	if ('download' in document.createElement('a')) { //非ie下载
		req.onload = function () {
			var data = req.response;
			var a = document.createComment("a");
			var blob = new Blob([data]);
			var blobUrl = window.URL.createObjectURL(blob);
			downloadFile(blobUrl, title);
		}
	} else {
		navigator.msSaveOrOpenBlob(blob, title)
	}
	req.send()
}
function downloadFile(blobUrl, title) {
	var a = document.createElement("a");
	document.body.appendChild(a)
	a.style.display = 'none';
	a.download = title;
	a.href = blobUrl;
	a.click();
	document.body.removeChild(a);
}


/**
 * @function base64编码方式
 */
;(function($) {
    
        var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
            a256 = '',
            r64 = [256],
            r256 = [256],
            i = 0;
    
        var UTF8 = {
    
            /**
             * Encode multi-byte Unicode string into utf-8 multiple single-byte characters
             * (BMP / basic multilingual plane only)
             *
             * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
             *
             * @param {String} strUni Unicode string to be encoded as UTF-8
             * @returns {String} encoded string
             */
            encode: function(strUni) {
                // use regular expressions & String.replace callback function for better efficiency
                // than procedural approaches
                var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
                function(c) {
                    var cc = c.charCodeAt(0);
                    return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f);
                })
                .replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
                function(c) {
                    var cc = c.charCodeAt(0);
                    return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f);
                });
                return strUtf;
            },
    
            /**
             * Decode utf-8 encoded string back into multi-byte Unicode characters
             *
             * @param {String} strUtf UTF-8 string to be decoded back to Unicode
             * @returns {String} decoded string
             */
            decode: function(strUtf) {
                // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
                var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
                function(c) { // (note parentheses for precence)
                    var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f);
                    return String.fromCharCode(cc);
                })
                .replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
                function(c) { // (note parentheses for precence)
                    var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f;
                    return String.fromCharCode(cc);
                });
                return strUni;
            }
        };
    
        while(i < 256) {
            var c = String.fromCharCode(i);
            a256 += c;
            r256[i] = i;
            r64[i] = b64.indexOf(c);
            ++i;
        }
    
        function code(s, discard, alpha, beta, w1, w2) {
            s = String(s);
            var buffer = 0,
                i = 0,
                length = s.length,
                result = '',
                bitsInBuffer = 0;
    
            while(i < length) {
                var c = s.charCodeAt(i);
                c = c < 256 ? alpha[c] : -1;
    
                buffer = (buffer << w1) + c;
                bitsInBuffer += w1;
    
                while(bitsInBuffer >= w2) {
                    bitsInBuffer -= w2;
                    var tmp = buffer >> bitsInBuffer;
                    result += beta.charAt(tmp);
                    buffer ^= tmp << bitsInBuffer;
                }
                ++i;
            }
            if(!discard && bitsInBuffer > 0) result += beta.charAt(buffer << (w2 - bitsInBuffer));
            return result;
        }
    
        var Plugin = $.base64 = function(dir, input, encode) {
                return input ? Plugin[dir](input, encode) : dir ? null : this;
            };
    
        Plugin.btoa = Plugin.encode = function(plain, utf8encode) {
            plain = Plugin.raw === false || Plugin.utf8encode || utf8encode ? UTF8.encode(plain) : plain;
            plain = code(plain, false, r256, b64, 8, 6);
            return plain + '===='.slice((plain.length % 4) || 4);
        };
    
        Plugin.atob = Plugin.decode = function(coded, utf8decode) {
            coded = coded.replace(/[^A-Za-z0-9\+\/\=]/g, "");
            coded = String(coded).split('=');
            var i = coded.length;
            do {--i;
                coded[i] = code(coded[i], true, r64, a256, 6, 8);
            } while (i > 0);
            coded = coded.join('');
            return Plugin.raw === false || Plugin.utf8decode || utf8decode ? UTF8.decode(coded) : coded;
        };
    }(jQuery));

    /*
    * 给定一个精度值，计算某一项在一串数据中占据的百分比，确保百分比总和是1（100%）
    * 使用最大余额法
    * Get a data of given precision, assuring the sum of percentages
    * in valueList is 1.
    * The largest remainer method is used.
    * https://en.wikipedia.org/wiki/Largest_remainder_method
    *
    * @param {Array.<number>} valueList a list of all data 一列数据
    * @param {number} idx index of the data to be processed in valueList 索引值（数组下标）
    * @param {number} precision integer number showing digits of precision 精度值
    * @return {number} percent ranging from 0 to 100 返回百分比从0到100
    */
         function getPercentWithPrecision(valueList, idx, precision) {
             if (!valueList[idx]) {
                 return 0
             }
 
             var sum = valueList.reduce(function (acc, val) {
                 return acc + (isNaN(val) ? 0 : val)
             }, 0)
             if (sum === 0) {
                 return 0
             }
             // sum 9
             var digits = Math.pow(10, precision) // digits 100
             var votesPerQuota = valueList.map(function (val) {
                 return (isNaN(val) ? 0 : val) / sum * digits * 100 // 扩大比例，这样可以确保整数部分是已经确定的议席配额，小数部分是余额
             })
             // votesPerQuota [ 2222.222222222222, 4444.444444444444, 3333.333333333333 ] 每一个项获得的议席配额，整数部分是已经确定的议席配额，小数部分是余额
             var targetSeats = digits * 100 // targetSeats 10000 全部的议席
             var seats = votesPerQuota.map(function (votes) {
                 // Assign automatic seats.
                 return Math.floor(votes)
             })
             // seats [ 2222, 4444, 3333 ] 获取配额的整数部分
             var currentSum = seats.reduce(function (acc, val) {
                 return acc + val
             }, 0)
             // 9999 表示已经配额了9999个议席，还剩下一个议席
             var remainder = votesPerQuota.map(function (votes, idx) {
                 return votes - seats[idx]
             })
             // [ 0.2222222222221717, 0.4444444444443434, 0.33333333333303017 ]得到每一项的余额
             // Has remainding votes. 如果还有剩余的坐席就继续分配
             while (currentSum < targetSeats) {
                 // Find next largest remainder. 找到下一个最大的余额
                 var max = Number.NEGATIVE_INFINITY
                 var maxId = null
                 for (var i = 0, len = remainder.length; i < len; ++i) {
                     if (remainder[i] > max) {
                         max = remainder[i]
                         maxId = i
                     }
                 }
                 // max: 0.4444444444443434, maxId 1
                 // Add a vote to max remainder.
                 ++seats[maxId] // 第二项，即4的占比的坐席增加1
                 remainder[maxId] = 0
                 ++currentSum // 总的已分配的坐席数也加1
             }
 
             return seats[idx] / digits
         }
 